package com.glw.sso.platform.web.controller;

import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.glw.sso.platform.common.response.Response;
import com.glw.sso.platform.service.DemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api
@Slf4j
@RestController
@RequestMapping("/demo")
@Validated
public class DemoController {

    @Autowired
    DemoService demoService;

    @Operation(summary = "weather")
    @GetMapping("weather")
    public Response weather(@ApiParam("城市") String city) {
        return Response.succeeded(demoService.weatherQuery(city));
    }

    @Operation(summary = "cacheable-test")
    @GetMapping("cacheable-test")
    public Response cacheableTest(@ApiParam("aParam") String aParam) {
        return Response.succeeded(demoService.cacheableTest(aParam));
    }

    @Operation(summary = "caffeine-test")
    @GetMapping("caffeine-test")
    public Response caffeineTest(@ApiParam("aParam") String aParam) {
        return Response.succeeded(demoService.caffeineCache(aParam));
    }

    @Operation(summary = "caffeine-stats-test")
    @GetMapping("caffeine-stats-test")
    public Response caffeineStatsTest() {
        CacheStats cacheStats = demoService.caffeineCacheStats();
        return Response.succeeded(
                String.format("命中率 %s | 命中总次数 %s | 未命中总次数 %s | 逐出数量 %s | load平均时间花费 %s",
                        cacheStats.hitRate(),
                        cacheStats.hitCount(),
                        cacheStats.missCount(),
                        cacheStats.evictionCount(),
                        cacheStats.averageLoadPenalty()
                ));
    }
}
